// SPDX-License-Identifier: GPL-2.0
/*
 * BUDDY GFP: GFP_USER
 *
 * (C) 2024.05.12 <buddy.zhang@aliyun.com>
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/ioctl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/mman.h>

#define DEV_PATH		"/dev/BiscuitOS"
#define MAP_VADDR		0x600000000
#define MAP_SIZE		0x1000

/* EXCHANGE DATA BETWEEN KERNEL AND USERLAND */
struct BiscuitOS_data {
	char *buffer;
};

int main()
{
	struct BiscuitOS_data bdata;
	int fd;

	/* OPEN DEVICE */
	fd = open(DEV_PATH, O_RDWR);
	if (fd < 0) {
		printf("ERROR: Can't open %s\n", DEV_PATH);
		return -1;
	}

	/* ALLOC MEMORY */
	bdata.buffer = mmap((void *)MAP_VADDR, MAP_SIZE,
			    PROT_READ | PROT_WRITE,
			    MAP_ANONYMOUS | MAP_PRIVATE,
			    -1,
			    0);
	if (bdata.buffer == MAP_FAILED) {
		printf("ERROR: MMAP FAILED\n");
		exit(-1);
	}

	/* WRITE DATA INTO KERNEL SPACE */
	sprintf(bdata.buffer, "Hello BiscuitOS!");
	write(fd, bdata.buffer, MAP_SIZE);

	/* RECLAIM */
	munmap(bdata.buffer, MAP_SIZE);
	close(fd);

	return 0;
}
